//
//  UIWindow+Extension.m
//  黑马微博2期
//
//  Created by 龚啸 on 15/8/25.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "UIWindow+Extension.h"
#import "GXNewfeatureViewController.h"

@implementation UIWindow (Extension)

- (void)changeRootViewController:(UIViewController *)wrapperMainVC
{
    UINavigationController *nav = [[UINavigationController alloc]init];
    NSString *key = @"CFBundleVersion";
    // 上一次的使用版本（存储在沙盒中的版本号）
    NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    // 当前软件的版本号（从Info.plist中获得）
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
       if ([currentVersion isEqualToString:lastVersion]) { // 版本号相同：这次打开和上次打开的是同一个版本
           AdViewController *adVc = [[AdViewController alloc]init];
          
           if (adVc && wrapperMainVC == nil) {
              
               [nav addChildViewController:adVc];
                self.rootViewController = nav;
    
           }else{
//               [nav addChildViewController:wrapperMainVC];
               self.rootViewController = wrapperMainVC;
           }
    } else { // 这次打开的版本和上一次不一样，显示新特性
        
        /**
         *  移动文件 以后可删除
         */
        NSString *filePath = [NSString stringWithFormat:@"%@/Documents/lyss",NSHomeDirectory()];
        NSString *document = [NSString stringWithFormat:@"%@/Library/Caches/lyss",NSHomeDirectory()];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        if ([fileManager fileExistsAtPath:filePath]) {
            
            if ([fileManager fileExistsAtPath:document]) {
                [fileManager removeItemAtPath:document error:nil];
            }
            
            [fileManager moveItemAtPath:filePath toPath:document error:nil];
        }
        
      GXNewfeatureViewController *vc = [[GXNewfeatureViewController alloc] init];
       [nav addChildViewController:vc];
        [vc.navigationController setNavigationBarHidden:YES];
        // 将当前的版本号存进沙盒
        [[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:key];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        self.rootViewController = nav;
    }
    
    
}

@end
